from utils.check_params_utlil import CheckParams
from utils.handler_util import HandlerUtil
from handlers.base_handler import BaseHandler
# from auths.decorator import authenticated
from blls.project_blls.project_bll import ProjectBLL


class SplitDatasetHandler(BaseHandler):
    # @authenticated
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["project_id", True, ""],
                                          ["split", True, {}]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        project_id, split = check_r.data.get('project_id'), check_r.data.get('split')
        res = ProjectBLL(request=self).split_dataset(project_id, split)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success))


class ExportDatasetHandler(BaseHandler):
    # @authenticated
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["project_id", True, ""],
                                          ["export_format", True, ""],
                                          ["export_dir", True, ""],
                                          ["seg_mask_type", False, ""],
                                          ["finalValues",False,""]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        project_id, export_format = check_r.data.get('project_id'), check_r.data.get('export_format')
        export_dir, seg_mask_type, finalValues = check_r.data.get('export_dir'), check_r.data.get('seg_mask_type'), check_r.data.get('finalValues')
        res = ProjectBLL(request=self).export_dataset(project_id, export_format, export_dir, seg_mask_type,finalValues)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success))


class CreateProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["name", False, ""],
            ["description", False, ""],
            ["task_category_id", False, ""],
            ["data_dir", False, ""],
            ["user_id", False, ""],
            ["labels", False, []],
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).create_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class DeleteProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["id", True, ""]
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).delete_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class StateProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["project_id", True, ""]
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).state_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class EditProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["id", True, ""],
            ["name", False, ""],
            ["description", False, ""],
            ["labels", False, []]
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).edit_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class DeatilProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["project_id", False, ""],
            ["username", False, ""],
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).detail_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class ListProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["page_num", False, 1],
            ["page_size", False, 10],
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).list_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class UpdateProjectHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["path", False, ""],
            ["project_id", False, ""],
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).update_project(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))



class VersionHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).version(**res_data.data)
        return self.write(HandlerUtil.set_failure_response(data=res.data, message=res.message, status=res.code))

class QuantityHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["start_time", False, ""],
            ["end_time", False, ""],
            ["project_id", False, ""],
            ["username", False, ""],
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).ann_quantity(**res_data.data)
        return self.write(HandlerUtil.set_failure_response(data=res.data, message=res.message, status=res.code))

class ConformHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["data_id",False,""],
            ["status",False,""]
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ProjectBLL(request=self).conform(**res_data.data)
        return self.write(HandlerUtil.set_failure_response(data=res.data, message=res.message, status=res.code))
